package org.tloss.web.editor.model.command;

import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

import org.tloss.web.spring.FormMapping;
import org.tloss.web.spring.View;

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Reply extends SimpleCommand {
	public String returnCase;
	public String viewName;

	@XmlAttribute
	public String getViewName() {
		return viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	@XmlAttribute
	public String getReturnCase() {
		return returnCase;
	}

	public void setReturnCase(String returnCase) {
		this.returnCase = returnCase;
	}

	public String getLabel() {
		if (returnCase != null) {
			return "Reply[" + returnCase + "]";
		}
		return "Reply[]";
	}

	@Override
	public String generate(int type, Object[] data) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) data[0];
		FormMapping formMapping = (FormMapping) map.get("formMapping");
		View view = new View();
		view.setReturnCase(returnCase);
		if(viewName==null || viewName.equals("")){
			view.setViewName(formMapping.getName()+"_"+returnCase+".jsp");
		}else{
			view.setViewName(viewName);
		}
		formMapping.addView(view);
		return "return Utils.createView(getFormName(),\"" + returnCase + "\");";
	}
}
